// pages/score/score.ts
import { formatTime } from '../../utils/util'
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    scores: [] as any,
    total: 0,
    currentPage: 1
  },
  delScore(e: AnyObject) {
    const that = this
    wx.showModal({
      title: '提示',
      content: '确定要删除吗?',
      success(res) {
        if (res.confirm) {
          that.delSubmit(e.target.dataset.id)
          //这里是点击确认执行事件
        }
      }
    })
  },
  delSubmit(id: number) {
    const that = this
    const delRow = this.data.scores.find((item: any) => item.id === id)
    const { type, testMinute } = delRow
    wx.request({
      header: {
        Authorization: app.globalData.userInfo.token
      },
      url: `https://www.liqiantw.icu:3000/score/list/delete`,
      method: 'POST',
      data: { id: [id], type, testMinute },
      success: function (response) {
        if (response.statusCode === 200) {
          that.setData({ currentPage: 1 })
          that.getList().then(data => {
            that.setData({ scores: data })
          })
          wx.showToast({
            title: '删除成功',
            icon: 'success',
            duration: 1000
          })
        } else {
          wx.showToast({
            title: '删除失败',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '删除失败',
          icon: 'error',
          duration: 1000
        })
      }
    });
  },
  loadMore() {
    if (this.data.scores.length === this.data.total) {
      return
    }
    this.setData({ currentPage: this.data.currentPage + 1 })
    this.getList().then((data: any) => {
      this.setData({ scores: [...this.data.scores, ...data] })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
  },
  getList() {
    return new Promise((resolve, reject) => {
      const that = this
      const mid = {
        name: app.globalData.userInfo.openid,
        prop: 'date',
        order: 'desc',
        pageSize: 30,
        currentPage: this.data.currentPage
      }
      wx.request({
        header: {
          Authorization: app.globalData.userInfo.token
        },
        url: `https://www.liqiantw.icu:3000/score/selfList`, // 填入业务自定义路径和参数，根目录，就是 / 
        method: 'POST', // 按照自己的业务开发，选择对应的方法
        data: mid,
        success: function ({ data: { data, total } }: any) {
          data.forEach((item: any) => { item.date = formatTime(new Date(item.date)) })
          that.setData({ total: total })
          resolve(data)
        },
        fail: function () {
          reject([])
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({ currentPage: 1 })
    this.getList().then(data => {
      this.setData({ scores: data })
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
  }
})